// pages/apply/apply.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../api/index'),
    utils = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '', // 门槛礼包商品id
        loading: false,
        isShow: false,
        phone: '',
        toast: {
            visible: false,
            text: ''
        },
        status: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id || ''
        })
        this.reload();
    },

    reload() {
        // this.getNewerGiftBanner();
        this.setData({
            status: 0
        })
        this.getPopBanner();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    // 获取新人优惠券礼包
    async getNewerGiftBanner() {
        try {
            let res = await api.banner.getLatestBanner('home_giftbag'),
                data = res.data.ResData || {};
            this.setData({
                giftData: data,
                status: 2
            })
            console.log(res, 'gift banenr ')
        } catch (e) {
            console.log(e, '获取首页礼包失败')
            this.setData({
                status: 1
            })
        }
    },

    getUserInfo(e) {
        this.showPhone();
        console.log(e, '用户信息')
    },

    hidePhone() {
        this.setData({
            isShow: false
        })
    },

    showPhone() {
        this.setData({
            isShow: true
        })
    },

    inputPhone(e) {
        this.setData({
            phone: e.detail.value || ''
        })
    },

    // 输入手机号
    async loginAsSaler() {
        if (!globalData.exp.phone.test(this.data.phone)) {
            this.setData({
                toast: {
                    visible: true,
                    text: '请填写正确的手机号'
                }
            })
            return
        }
        let obj = {
            sessionId: wx.getStorageSync('ssId') || '',
            tell: this.data.phone
        }
        try {
            let res = await api.user.updatePhone(obj);
            console.log(res, '更新手机号')
            this.setData({
                toast: {
                    visible: true,
                    text: '申请成功'
                }
            })
        } catch (e) {
            this.setData({
                toast: {
                    visible: true,
                    text: '申请失败'
                }
            })
            console.log(e, '更新手机号失败')
        } finally {
            this.hidePhone()
        }
        // 更新用户信息（重新登录

        // wx.navigateTo({
        //     url: `/pages/product/detail/detail?id=${this.data.id}&userId=''`,
        // })
    },

    // 获取手机号
    async getPhoneNumber(e) {
        console.log(e, '手机号')
        let obj = {
            sessionId: wx.getStorageSync('ssId') || '',
            encryptedData: e.detail.encryptedData || '',
            iv: e.detail.iv
        }
        try {
            let res = await api.user.decodePhone(obj),
                data = res.data.ResData || '';
            console.log(res, '解密手机号')
        } catch (e) {
            console.log(e, '获取手机号失败')
        }
    },

    // 获取分销员推广计划
    async getPopBanner() {
        try {
            let res = await api.banner.getBanners('home_rules'),
                data = res.data.ResData || [];
            console.log(res, 'banners')
            this.setData({
                status: 2,
                giftData: data
            })
        } catch (e) {
            console.log(e, '获取分销员推广计划失败')
            this.setData({
                status: 1
            })
        }
    },

    // 去详情
    toDetail() {
        let uri = this.data.giftData[0].LinkUrl;
        if (uri) {
            wx.navigateTo({
                url: uri,
            })
        }
    }
})